<?php
namespace app\api\controller\rebate;

use app\api\controller\Baseuser;
use app\common\model\rebate\Rebate as RebateModel;
use app\common\model\rebate\RebateList;

use app\common\model\orderhawa\Hawa;
use app\common\model\orderhawa\HawaList;

class Orderhawa extends Baseuser
{
    //初始化
    public function initialize()
    {
        parent::initialize();
        // Rolepower::powerguard(101004);
    }
    public function index(){
    	echo "string";
    }

    /**
     * 创建服务费档案 createlist
     * @return [type] [description]
     */
    public function create()
    {
        //创建档案
        $data = input('post.');
        if (empty($data['HawaListId'])) {
            return moerr('缺少关联订单信息');
        }
        $sb = RebateList::where('CorrelationId', $data['HawaListId'])->where('IsDelete', '<>', '1')->where('Type', 'orderhawa')->find();
        if (empty($sb)) {
            $data['CorrelationId'] = $data['HawaListId'];
            $data['Type']          = 'orderhawa';
            $id                  = RebateList::user_create($data);
        } else {
            $id = $sb->RebateListId;
        }
        //填写危废子订单的信息
        if (empty($data['HawaId'])) {
            return myerr('缺少子订单数据');
        }
        $ndata['RebateListId'] = $id;
        $ndata['Price']        = $data['Price'];
        // $ndata['Limit']        = $data['Limit'];
        $ndata['RebateTime']   = $data['RebateTime'];
        $ndata['id'] = $id;
        RebateModel::user_create($ndata);
        //修改子订单的危废信息
        $hawasb        = Hawa::get($data['HawaId']);
        $hawasb->Limit = $data['Price'];
        $hawasb->save();
        
        if(!empty($sb)){
            RebateList::add_price($id,$data['Price']);
        }else{
            $sb = RebateList::get($id);
        }
        //判断总订单是否已经结束
        $hawalistsb = Hawa::where('HawaListId', $data['HawaListId'])->where('Limit','null')->find();
        if (empty($hawalistsb)) {
            //修改返点总订单状态
            $sb->State = RebateList::STATEEND;
            $sb->save();
        }
        return myok($id);
    }


}
